﻿using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Windows.Threading;
using Common;
using ImageProcessing.Storage;
using Visualizers.DataModels;

namespace Visualizers.ViewModels
{
    public class MainWindowViewModel : INotifyPropertyChanged
    {
        private readonly Dispatcher _dispatcher;
        private readonly ObservableCollection<TagInfoDataModel> _imageList = new ObservableCollection<TagInfoDataModel>();
        public ObservableCollection<TagInfoDataModel> ImageList { get { return _imageList; } }

        private TagInfoDataModel _selectedImage;
        public TagInfoDataModel SelectedImage
        {
            get { return _selectedImage; }
            set
            {
                if (_selectedImage == value)
                    return;

                _selectedImage = value;
                RaisePropertyChanged("SelectedImage");
            }
        }

        private readonly OnlineImageArchiver _onlineImageArchiver = new OnlineImageArchiver();
        private readonly BackGroundImageDownloader _backGroundImageDownloader;

        public event PropertyChangedEventHandler PropertyChanged;

        public string Status { get; private set; }

        public MainWindowViewModel(Dispatcher dispatcher)
        {
            _dispatcher = dispatcher;

            foreach (var tagInfo in _onlineImageArchiver.ImageList)
                AddTagInfoToModels(tagInfo);

            _backGroundImageDownloader = new BackGroundImageDownloader(_onlineImageArchiver);
            _backGroundImageDownloader.SatusUpdate += StatusUpdate;
            _backGroundImageDownloader.DownloadComplete += DownloadComplete;
            _backGroundImageDownloader.ImageAdded += AddImage;
        }

        private void AddImage(TagInfo tagInfo)
        {
            if (_imageList.FirstOrDefault(x => x.TagInfo == tagInfo) != null)
                return;

            AddTagInfoToModels(tagInfo);
        }

        private void AddTagInfoToModels(TagInfo tagInfo)
        {
            _dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action<object>((o) =>
                                         {
                                             var tdm = new TagInfoDataModel(tagInfo);
                                             _imageList.Add(tdm);
                                         }), null);
        }

        private void DownloadComplete()
        {
            StatusUpdate("Download Complete");
        }

        private void StatusUpdate(string status)
        {
            Status = status;
            RaisePropertyChanged("Status");
        }

        public void DownloadImagesFromServer()
        {
            _backGroundImageDownloader.AsyncDownload();
            StatusUpdate("Downloading images from server");
        }

        private void RaisePropertyChanged(string propertyName)
        {
            var evtPropertyChanged = PropertyChanged;
            if (evtPropertyChanged != null)
                evtPropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
